package com.gpnu7_412.yanzhi.controller.comment;

import com.gpnu7_412.yanzhi.dto.CommentDisplayDto;
import com.gpnu7_412.yanzhi.entity.Comment;
import com.gpnu7_412.yanzhi.entity.CommentHanding;
import com.gpnu7_412.yanzhi.exception.custom.RequestDataException;
import com.gpnu7_412.yanzhi.service.CommentService;
import com.gpnu7_412.yanzhi.util.Result;
import com.gpnu7_412.yanzhi.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZZH
 */
@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     * @param comment 评论对象
     * @return 响应对象
     */
    @PostMapping("/add")
    public Result addComment(@RequestBody Comment comment) {
        System.out.println(comment);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        comment.setUserId(userId);
        if (!commentService.addComment(comment)) {
            throw new RequestDataException("参数为空");
        }
        return new Result();
    }

    /**
     * 删除评论
     * @param commentId 评论ID
     * @return 响应对象
     */
    @DeleteMapping("/delete")
    public Result deleteComment(@RequestParam("commentId") Integer commentId) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        if (!commentService.deleteComment(commentId, userId)) {
            throw new RequestDataException("参数为空");
        }
        return new Result();
    }

    /**
     * 修改评论
     * @param comment 评论对象
     * @return 响应对象
     */
    @PutMapping("/update")
    public Result updateComment(@RequestBody Comment comment) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        if (!commentService.updateComment(comment, userId)) {
            throw new RequestDataException("参数为空");
        }
        return new Result();
    }

    // ?
    @PostMapping("/select/id")
    public Result selectCommentById(@RequestBody Map<String, Object> map) {
        Integer commentId = (Integer) map.get("commentId");
        Comment comment = commentService.getByCommentId(commentId);
        return new Result();
    }

    /**
     * 根据动态ID获取评论列表
     * @param dynamicId 动态ID
     * @return 响应对象
     */
    @GetMapping("/select/dynamic")
    public Result selectCommentByDynamic(@RequestParam("dynamicId") Integer dynamicId) {
        List<CommentDisplayDto> commentList = commentService.getByDynamicId(dynamicId);
        if (commentList == null) {
            throw new RequestDataException("参数为空");
        }
        Map<String, Object> responseMap = new HashMap<>(2);
        responseMap.put("totalCount", commentList.size());
        responseMap.put("list", commentList);
        return new Result(responseMap);
    }

    /**
     * 点赞评论
     * @param commentId 评论ID
     * @return 响应对象
     */
    @PostMapping("/like")
    public Result likeComment(@RequestParam Integer commentId) {
        if (!commentService.likeComment(commentId)) {
            throw new RequestDataException("参数为空");
        }
        return new Result();
    }

    /**
     * 举报评论
     * @param commentHanding 评论举报对象
     * @return 响应对象
     */
    @PostMapping("/report")
    public Result reportComment(@RequestBody CommentHanding commentHanding) {
        commentHanding.setHandingTime(new Timestamp(System.currentTimeMillis()));
        if (!commentService.reportComment(commentHanding)) {
            throw new RequestDataException("参数为空");
        }
        return new Result();
    }
}
